# 原理就是需要重新 __enter__ 和 __exit__ 函数

class File:
    def __init__(self, filename):
        f = open(filename, 'w', encoding='utf8')
        self.file = f

    def __enter__(self):
        print('enter')
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print('exit')
        self.file.close()

    def write(self, data):
        self.file.write(data)


# f =  File('test.txt')
# f.write('test\n')

with File('test.txt') as file:
    file.write('test22\n')
